package com.webmall.servlet.cate;
/**
 * 3用于添加商品分类的servlet
 */

import com.webmall.entity.CateEntity;
import com.webmall.entity.UserEntity;
import com.webmall.service.webmall_CateDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet( "/manageHtml/admin_doCateadd")
public class doCateadd extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //根据add界面的name参数进行内容获取
        int parentId = Integer.parseInt(request.getParameter("parentId"));
        String name = request.getParameter("name");


        CateEntity cate = new CateEntity(0,name,parentId);


        //加入数据库的表中
        int count= webmall_CateDao.insertCate(cate);


        //成功或失败重定位到哪儿
        if (count >0){
            response.sendRedirect("/manageHtml/admin_CateSelect");//成功

        }else {//失败 用输出流输出 js语句
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('用户添加失败')");
            out.write("location.href='manageHtml/admin_cateadd.jsp'");
            out.write("</script>");

        }
    }

}
